#include "../../mqServer/consumer.hpp"
#include <gtest/gtest.h>

using namespace mq;

ConsumerManager::ptr cmp;
string qname = "queue1";
class ConsumerTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        cout << "所有单元测试前执行环境初始化" << endl;
        cmp = make_shared<ConsumerManager>();
        cmp->initQueueConsumer(qname);
    }
    virtual void TearDown() override
    {
        cout << "所有单元测试后执行环境清理" << endl;
        cmp->clear();
    }
};

void cb(const string &tag, const BasicProperties *bp, const string &body)
{
    cout << "消费了消息:" << tag << endl;
}

TEST(consumer_test, create_test)
{
    ASSERT_EQ(cmp->empty(qname), true);
    cmp->create(qname, "consumer1", true, cb);
    cmp->create(qname, "consumer2", true, cb);
    cmp->create(qname, "consumer3", true, cb);
    ASSERT_EQ(cmp->empty(qname), false);

    ASSERT_EQ(cmp->exists(qname, "consumer1"), true);
    ASSERT_EQ(cmp->exists(qname, "consumer2"), true);
    ASSERT_EQ(cmp->exists(qname, "consumer3"), true);
}

TEST(consumer_test, choose_test)
{
    ASSERT_EQ(cmp->choose(qname)->_tag, "consumer1");
    ASSERT_EQ(cmp->choose(qname)->_tag, "consumer2");
    ASSERT_EQ(cmp->choose(qname)->_tag, "consumer3");
    ASSERT_EQ(cmp->choose(qname)->_tag, "consumer1");
}

TEST(consumer_test, remove_test)
{
    cmp->remove(qname, "consumer1");
    ASSERT_EQ(cmp->exists(qname, "consumer1"), false);
    cmp->remove(qname, "consumer2");
    cmp->remove(qname, "consumer3");

    ASSERT_EQ(cmp->empty(qname), true);
}

TEST(consumer_test, init_destroy_test)
{
    cmp->initQueueConsumer("queue2");
    ASSERT_EQ(cmp->empty("queue2"), true);
    cmp->destroyQueueConsumer("queue2");
    ASSERT_EQ(cmp->empty("queue2"), false);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ConsumerTest);
    RUN_ALL_TESTS();
    return 0;
}